/**
 * created by 小卷毛, 2020-07-15 015
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.common_ui.utils

import android.content.Context
import android.view.View
import android.widget.TextView
import com.weigan.loopview.LoopView
import com.zhiyou.aifeng.common.base.*
import com.zhiyou.aifeng.common.helper.TimeHelper
import com.zhiyou.aifeng.common_ui.R
import com.zhiyou.aifeng.common_ui.base.BottomDialog
import java.util.*
import kotlin.collections.ArrayList

/**
 * 时间选择器（R.layout.dialog_select_more 布局文件）
 * 返回格式 yyyy-MM-dd HH:mm
 *
 * @param minYearValue    最小年份
 * @param maxYearValue    最大年份
 * @param count           年、月、日、时、分、秒的显示个数
 * @param title           标题
 * @param initYear        默认年
 * @param initMonth       默认月
 * @param initDay         默认日
 * @param initHour        默认时
 * @param initMinute      默认分
 * @param isCurrentDate   是否默认选择当天日期（与默认年、月、日互斥）
 * @param isBeforeLimited 是否限制选择该年内，当天后的日期
 * @param isAfterLimited  是否限制选择该年内，当天前的日期（前后日期互斥）
 * @param callback        回调接口
 */
fun Context.showTimeDialog(
    minYearValue: Int = 1900,
    maxYearValue: Int = 2100,
    count: Int = 5,
    title: String = "请选择",
    initYear: Int = 0,
    initMonth: Int = 0,
    initDay: Int = 0,
    initHour: Int = -1,
    initMinute: Int = -1,
    isCurrentDate: Boolean = true,
    isBeforeLimited: Boolean = false,
    isAfterLimited: Boolean = false,
    callback: (Int, Int, Int, Int, Int, String) -> Unit = { _, _, _, _, _, _ -> }
) {

    val dialog = object : BottomDialog(this) {

        private lateinit var loopYear: LoopView
        private lateinit var loopMonth: LoopView
        private lateinit var loopDay: LoopView
        private lateinit var loopHour: LoopView
        private lateinit var loopMinute: LoopView

        private val calendar = Calendar.getInstance()
        private val yearNow = calendar.get(Calendar.YEAR)
        private val monthNow = calendar.get(Calendar.MONTH) + 1
        private val dayNow = calendar.get(Calendar.DAY_OF_MONTH)

        private val listBig = listOf(1, 3, 5, 7, 8, 10, 12)
        private val listLittle = listOf(4, 6, 9, 11)

        private val listYear = ArrayList<Int>()
        private val listMonth = ArrayList<Int>()
        private val listDay = ArrayList<Int>()

        override fun onCreateView(): View {
            val view = inflate<View>(R.layout.dialog_select_more)

            val tvTitle = view.findViewById<TextView>(R.id.tv_dialog_select_title)
            val tvCancel = view.findViewById<TextView>(R.id.tv_dialog_select_cancle)
            val tvOk = view.findViewById<TextView>(R.id.tv_dialog_select_ok)

            loopYear = view.find(R.id.lv_dialog_select_year)
            loopMonth = view.find(R.id.lv_dialog_select_month)
            loopDay = view.find(R.id.lv_dialog_select_day)
            loopHour = view.find(R.id.lv_dialog_select_hour)
            loopMinute = view.find(R.id.lv_dialog_select_minute)

            loopYear.setTextSize(15f)
            loopMonth.setTextSize(15f)
            loopDay.setTextSize(15f)
            loopHour.setTextSize(15f)
            loopMinute.setTextSize(15f)

            loopYear.setDividerColor(getColorEx(R.color.divider))
            loopMonth.setDividerColor(getColorEx(R.color.divider))
            loopDay.setDividerColor(getColorEx(R.color.divider))
            loopHour.setDividerColor(getColorEx(R.color.divider))
            loopMinute.setDividerColor(getColorEx(R.color.divider))

            loopYear.setNotLoop()
            loopMonth.setNotLoop()
            loopDay.setNotLoop()
            loopHour.setNotLoop()
            loopMinute.setNotLoop()

            when (count) {
                1 -> {
                    loopMonth.gone()
                    loopDay.gone()
                    loopHour.gone()
                    loopMinute.gone()
                }
                2 -> {
                    loopDay.gone()
                    loopHour.gone()
                    loopMinute.gone()
                }
                3 -> {
                    loopHour.gone()
                    loopMinute.gone()
                }
                4 -> loopMinute.gone()
            }

            tvTitle.text = title
            tvCancel.oneClick { dismiss() }

            tvOk.oneClick {

                val year = listYear[loopYear.selectedItem]
                val month = listMonth[loopMonth.selectedItem]
                val day = listDay[loopDay.selectedItem]
                val hour = loopHour.selectedItem
                val minute = loopMinute.selectedItem

                var dateNew: String

                when (count) {
                    1 -> dateNew = year.toString() + "年"
                    2 -> {
                        dateNew = if (month < 10) "$year-0$month" else "$year-$month"
                    }
                    3 -> {
                        dateNew = when {
                            month < 10 && day < 10 -> "$year-0$month-0$day"
                            month < 10 && day >= 10 -> "$year-0$month-$day"
                            month >= 10 && day < 10 -> "$year-$month-0$day"
                            else -> "$year-$month-$day"
                        }
                    }
                    4 -> {
                        dateNew = when {
                            month < 10 && day < 10 -> "$year-0$month-0$day ${hour}时"
                            month < 10 && day >= 10 -> "$year-0$month-$day ${hour}时"
                            month >= 10 && day < 10 -> "$year-$month-0$day ${hour}时"
                            else -> "$year-$month-$day ${hour}时"
                        }
                    }
                    else -> {
                        dateNew = when {
                            month < 10 && day < 10 -> "$year-0$month-0$day"
                            month < 10 && day >= 10 -> "$year-0$month-$day"
                            month >= 10 && day < 10 -> "$year-$month-0$day"
                            else -> "$year-$month-$day"
                        }

                        when {
                            hour < 10 && minute < 10 -> dateNew += " 0$hour:0$minute"
                            hour < 10 && minute >= 10 -> dateNew += " 0$hour:$minute"
                            hour >= 10 && minute < 10 -> dateNew += " $hour:0$minute"
                            hour >= 10 && minute >= 10 -> dateNew += " $hour:$minute"
                        }
                    }
                }

                dismiss()
                callback.invoke(year, month, day, hour, minute, dateNew)
            }

            return view
        }

        override fun setUiBeforShow() {
            if (count > 3) {
                loopHour.setItems(dateToList(0, 23, "%d时"))
                if (initHour > -1) loopHour.setInitPosition(initHour)
            }
            if (count > 4) {
                loopMinute.setItems(dateToList(0, 59, "%d分"))
                if (initMinute > -1) loopMinute.setInitPosition(initMinute)
            }

            when {
                isBeforeLimited -> {
                    loopYear.setItems(dateToList(yearNow, maxYearValue, "%d年", listYear))

                    if (isCurrentDate) {
                        if (count > 1)
                            loopMonth.setItems(dateToList(monthNow, 12, "%d月", listMonth))

                        if (count > 2) {
                            when (monthNow) {
                                in listBig ->
                                    loopDay.setItems(dateToList(dayNow, 31, "%d日", listDay))
                                in listLittle ->
                                    loopDay.setItems(dateToList(dayNow, 30, "%d日", listDay))
                                else -> {
                                    if (TimeHelper.getInstance().isLeapYear(yearNow)) {
                                        loopDay.setItems(dateToList(dayNow, 29, "%d日", listDay))
                                    } else {
                                        loopDay.setItems(dateToList(dayNow, 28, "%d日", listDay))
                                    }
                                }
                            }
                        }
                    } else {
                        when (count) {
                            1 -> {
                                if (initYear in yearNow..maxYearValue)
                                    loopYear.setInitPosition(initYear - yearNow)
                            }
                            2 -> {
                                if (initYear in yearNow..maxYearValue && initMonth > 0) {
                                    if (initYear == yearNow) {
                                        loopMonth.setItems(
                                            dateToList(
                                                monthNow,
                                                12,
                                                "%d月",
                                                listMonth
                                            )
                                        )
                                        if (initMonth > monthNow) loopMonth.setInitPosition(
                                            initMonth - monthNow
                                        )
                                    } else {
                                        loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                        loopYear.setInitPosition(initYear - yearNow)
                                        loopMonth.setInitPosition(initMonth - 1)
                                    }
                                } else {
                                    loopMonth.setItems(dateToList(monthNow, 12, "%d月", listMonth))
                                }
                            }
                            3 -> {
                                if (initYear in yearNow..maxYearValue && initMonth > 0 && initDay > 0) {
                                    if (initYear == yearNow) {
                                        loopMonth.setItems(
                                            dateToList(
                                                monthNow,
                                                12,
                                                "%d月",
                                                listMonth
                                            )
                                        )

                                        if (initMonth > monthNow) {
                                            when (initMonth) {
                                                in listBig ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            31,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                in listLittle ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            30,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                else -> {
                                                    if (TimeHelper.getInstance()
                                                            .isLeapYear(yearNow)
                                                    ) {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                1,
                                                                29,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    } else {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                1,
                                                                28,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    }
                                                }
                                            }

                                            loopMonth.setInitPosition(initMonth - monthNow)
                                            loopDay.setInitPosition(initDay - 1)
                                        } else {
                                            when (monthNow) {
                                                in listBig ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            dayNow,
                                                            31,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                in listLittle ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            dayNow,
                                                            30,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                else -> {
                                                    if (TimeHelper.getInstance()
                                                            .isLeapYear(yearNow)
                                                    ) {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                dayNow,
                                                                29,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    } else {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                dayNow,
                                                                28,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    }
                                                }
                                            }

                                            if (initMonth == monthNow
                                                && initDay > dayNow
                                            )
                                                loopDay.setInitPosition(initDay - dayNow)
                                        }
                                    } else {
                                        loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                        when (initDay) {
                                            in listBig ->
                                                loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                            in listLittle ->
                                                loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                            else -> {
                                                if (TimeHelper.getInstance().isLeapYear(initYear)) {
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            29,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                } else {
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            28,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                }
                                            }
                                        }

                                        loopYear.setInitPosition(initYear - yearNow)
                                        loopMonth.setInitPosition(initMonth - 1)
                                        loopDay.setInitPosition(initDay - 1)
                                    }
                                } else {
                                    loopMonth.setItems(dateToList(monthNow, 12, "%d月", listMonth))
                                    when (monthNow) {
                                        in listBig ->
                                            loopDay.setItems(dateToList(dayNow, 31, "%d日", listDay))
                                        in listLittle ->
                                            loopDay.setItems(dateToList(dayNow, 30, "%d日", listDay))
                                        else -> {
                                            if (TimeHelper.getInstance().isLeapYear(yearNow)) {
                                                loopDay.setItems(
                                                    dateToList(
                                                        dayNow,
                                                        29,
                                                        "%d日",
                                                        listDay
                                                    )
                                                )
                                            } else {
                                                loopDay.setItems(
                                                    dateToList(
                                                        dayNow,
                                                        28,
                                                        "%d日",
                                                        listDay
                                                    )
                                                )
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                isAfterLimited -> {
                    loopYear.setItems(dateToList(minYearValue, yearNow, "%d年", listYear))

                    if (isCurrentDate) {
                        loopYear.setInitPosition(yearNow - minYearValue)

                        if (count > 1) {
                            loopMonth.setItems(dateToList(1, monthNow, "%d月", listMonth))
                            loopMonth.setInitPosition(monthNow - 1)
                        }

                        if (count > 2) {
                            loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))
                            loopDay.setInitPosition(dayNow - 1)
                        }
                    } else {
                        when (count) {
                            1 -> {
                                if (initYear in minYearValue..yearNow)
                                    loopYear.setInitPosition(initYear - minYearValue)
                            }
                            2 -> {
                                if (initYear in minYearValue..yearNow && initMonth > 0) {
                                    if (initYear == yearNow) {
                                        loopMonth.setItems(
                                            dateToList(
                                                1,
                                                monthNow,
                                                "%d月",
                                                listMonth
                                            )
                                        )

                                        loopYear.setInitPosition(initYear - minYearValue)
                                        if (initMonth <= monthNow)
                                            loopMonth.setInitPosition(initMonth - 1)
                                    } else {
                                        loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                        loopYear.setInitPosition(initYear - minYearValue)
                                        loopMonth.setInitPosition(initMonth - 1)
                                    }
                                } else {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                }
                            }
                            3 -> {
                                if (initYear in minYearValue..yearNow && initMonth > 0 && initDay > 0) {
                                    if (initYear == yearNow) {
                                        loopMonth.setItems(
                                            dateToList(
                                                1,
                                                monthNow,
                                                "%d月",
                                                listMonth
                                            )
                                        )

                                        if (initMonth < monthNow) {
                                            when (initMonth) {
                                                in listBig ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            31,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                in listLittle ->
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            30,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                else -> {
                                                    if (TimeHelper.getInstance()
                                                            .isLeapYear(initYear)
                                                    ) {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                1,
                                                                29,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    } else {
                                                        loopDay.setItems(
                                                            dateToList(
                                                                1,
                                                                28,
                                                                "%d日",
                                                                listDay
                                                            )
                                                        )
                                                    }
                                                }
                                            }

                                            loopYear.setInitPosition(initYear - minYearValue)
                                            loopMonth.setInitPosition(initMonth - 1)
                                            loopDay.setInitPosition(initDay - 1)
                                        } else {
                                            loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))

                                            loopYear.setInitPosition(initYear - minYearValue)
                                            loopMonth.setInitPosition(monthNow - 1)
                                            if (initMonth == monthNow && initDay < dayNow)
                                                loopDay.setInitPosition(initDay - 1)
                                            else loopDay.setInitPosition(dayNow - 1)
                                        }
                                    } else {
                                        loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                        when (initMonth) {
                                            in listBig ->
                                                loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                            in listLittle ->
                                                loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                            else -> {
                                                if (TimeHelper.getInstance().isLeapYear(initYear)) {
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            29,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                } else {
                                                    loopDay.setItems(
                                                        dateToList(
                                                            1,
                                                            28,
                                                            "%d日",
                                                            listDay
                                                        )
                                                    )
                                                }
                                            }
                                        }

                                        loopYear.setInitPosition(initYear - minYearValue)
                                        loopMonth.setInitPosition(initMonth - 1)
                                        loopDay.setInitPosition(initDay - 1)
                                    }
                                } else {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                    loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                }
                            }
                        }
                    }
                }
                else -> {
                    loopYear.setItems(dateToList(minYearValue, maxYearValue, "%d年", listYear))

                    if (isCurrentDate) {
                        loopYear.setInitPosition(yearNow - minYearValue)

                        if (count > 1) {
                            loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                            loopMonth.setInitPosition(monthNow - 1)
                        }

                        if (count > 2) {
                            when (monthNow) {
                                in listBig ->
                                    loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                in listLittle ->
                                    loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                else -> {
                                    if (TimeHelper.getInstance().isLeapYear(yearNow)) {
                                        loopDay.setItems(dateToList(1, 29, "%d日", listDay))
                                    } else {
                                        loopDay.setItems(dateToList(1, 28, "%d日", listDay))
                                    }
                                }
                            }

                            loopDay.setInitPosition(dayNow - 1)
                        }
                    } else {
                        when (count) {
                            1 -> {
                                if (initYear in minYearValue..maxYearValue)
                                    loopYear.setInitPosition(initYear - minYearValue)
                            }
                            2 -> {
                                if (initYear in minYearValue..maxYearValue && initMonth > 0) {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                    loopYear.setInitPosition(initYear - minYearValue)
                                    loopMonth.setInitPosition(initMonth - 1)
                                } else {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                }
                            }
                            3 -> {
                                if (initYear in minYearValue..maxYearValue && initMonth > 0 && initDay > 0) {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                    when (initMonth) {
                                        in listBig ->
                                            loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                        in listLittle ->
                                            loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                        else -> {
                                            if (TimeHelper.getInstance().isLeapYear(initYear)) {
                                                loopDay.setItems(dateToList(1, 29, "%d日", listDay))
                                            } else {
                                                loopDay.setItems(dateToList(1, 28, "%d日", listDay))
                                            }
                                        }
                                    }

                                    loopYear.setInitPosition(initYear - minYearValue)
                                    loopMonth.setInitPosition(initMonth - 1)
                                    loopDay.setInitPosition(initDay - 1)
                                } else {
                                    loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                                    loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                                }
                            }
                        }
                    }
                }
            }

            loopMonth.setListener {
                val yearNum = listYear[loopYear.selectedItem]
                val monthNum = listMonth[loopMonth.selectedItem]
                initDay(yearNum, monthNum)
            }

            loopYear.setListener {
                if (count > 1) {
                    val yearNum = listYear[loopYear.selectedItem]
                    initMonth(yearNum)
                }
            }
        }

        /*
         * 根据年、限制条件来判断"月"的数据
         */
        private fun initMonth(year: Int) {
            val currentMonth = listMonth[loopMonth.selectedItem]
            if (year == yearNow) {
                when {
                    isBeforeLimited -> {
                        loopMonth.setItems(dateToList(monthNow, 12, "%d月", listMonth))
                        if (currentMonth < monthNow) {
                            loopMonth.setCurrentPosition(0)
                            if (count > 2) initDay(year, monthNow)
                        } else {
                            loopMonth.setCurrentPosition(currentMonth - monthNow)
                            if (count > 2) initDay(year, currentMonth)
                        }
                    }
                    isAfterLimited -> {
                        loopMonth.setItems(dateToList(1, monthNow, "%d月", listMonth))
                        if (currentMonth > monthNow) {
                            loopMonth.setCurrentPosition(monthNow - 1)
                            if (count > 2) initDay(year, monthNow)
                        } else {
                            if (count > 2) initDay(year, currentMonth)
                        }
                    }
                    else -> {
                        loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                        if (count > 2) initDay(year, currentMonth)
                    }
                }
            } else {
                loopMonth.setItems(dateToList(1, 12, "%d月", listMonth))
                if (count > 2) initDay(year, currentMonth)
            }
        }

        /*
         * 根据年(是否闰年)、月(大小月)、限制条件来判断"日"的数据
         */
        private fun initDay(year: Int, month: Int) {
            val currentDay = listDay[loopDay.selectedItem]
            when (month) {
                in listBig -> {
                    if (year == yearNow && month == monthNow) {
                        when {
                            isBeforeLimited -> {
                                loopDay.setItems(dateToList(dayNow, 31, "%d日", listDay))
                                if (currentDay < dayNow) loopDay.setCurrentPosition(0)
                                else loopDay.setCurrentPosition(currentDay - dayNow)
                            }
                            isAfterLimited -> {
                                loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))
                                if (currentDay > dayNow) loopDay.setCurrentPosition(dayNow - 1)
                            }
                            else -> {
                                loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                            }
                        }
                    } else {
                        loopDay.setItems(dateToList(1, 31, "%d日", listDay))
                    }
                }
                in listLittle -> {
                    if (year == yearNow && month == monthNow) {
                        when {
                            isBeforeLimited -> {
                                loopDay.setItems(dateToList(dayNow, 30, "%d日", listDay))
                                if (currentDay < dayNow) loopDay.setCurrentPosition(0)
                                else {
                                    if (currentDay == 31) loopDay.setCurrentPosition(30 - dayNow)
                                    else loopDay.setCurrentPosition(currentDay - dayNow)
                                }
                            }
                            isAfterLimited -> {
                                loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))
                                if (currentDay > dayNow) loopDay.setCurrentPosition(dayNow - 1)
                            }
                            else -> {
                                loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                if (currentDay == 31) loopDay.setCurrentPosition(29)
                            }
                        }
                    } else {
                        loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                        if (currentDay == 31) loopDay.setCurrentPosition(29)
                    }
                }
                else -> {
                    if (TimeHelper.getInstance().isLeapYear(year)) {
                        if (year == yearNow && month == monthNow) {
                            when {
                                isBeforeLimited -> {
                                    loopDay.setItems(dateToList(dayNow, 29, "%d日", listDay))
                                    if (currentDay < dayNow) loopDay.setCurrentPosition(0)
                                    else {
                                        if (currentDay > 29) loopDay.setCurrentPosition(29 - dayNow)
                                        else loopDay.setCurrentPosition(currentDay - dayNow)
                                    }
                                }
                                isAfterLimited -> {
                                    loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))
                                    if (currentDay > dayNow) loopDay.setCurrentPosition(dayNow - 1)
                                }
                                else -> {
                                    loopDay.setItems(dateToList(1, 30, "%d日", listDay))
                                    if (currentDay > 29) loopDay.setCurrentPosition(28)
                                }
                            }
                        } else {
                            loopDay.setItems(dateToList(1, 29, "%d日", listDay))
                            if (currentDay > 29) loopDay.setCurrentPosition(28)
                        }
                    } else {
                        if (year == yearNow && month == monthNow) {
                            when {
                                isBeforeLimited -> {
                                    loopDay.setItems(dateToList(dayNow, 28, "%d日", listDay))
                                    if (currentDay < dayNow) loopDay.setCurrentPosition(0)
                                    else {
                                        if (currentDay > 28) loopDay.setCurrentPosition(28 - dayNow)
                                        else loopDay.setCurrentPosition(currentDay - dayNow)
                                    }
                                }
                                isAfterLimited -> {
                                    loopDay.setItems(dateToList(1, dayNow, "%d日", listDay))
                                    if (currentDay > dayNow) loopDay.setCurrentPosition(dayNow - 1)
                                }
                                else -> {
                                    loopDay.setItems(dateToList(1, 28, "%d日", listDay))
                                    if (currentDay > 28) loopDay.setCurrentPosition(27)
                                }
                            }
                        } else {
                            loopDay.setItems(dateToList(1, 28, "%d日", listDay))
                            if (currentDay > 28) loopDay.setCurrentPosition(27)
                        }
                    }
                }
            }
        }

    }

    dialog.show()
}

private fun dateToList(
    minValue: Int,
    maxValue: Int,
    format: String = "",
    list: ArrayList<Int>? = null
): List<String> {
    val items = ArrayList<String>()
    list?.clear()

    for (value in minValue..maxValue) {
        list?.add(value)
        items.add(
            if (format.isEmpty()) value.toString()
            else String.format(format, value)
        )
    }
    return items
}
